package core;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import core.codeGeneration.CodeGenerator;
import core.error.ErrorHandler;
import core.error.SyntaxException;
import core.lexical.LexicalAnalyser;
import core.semantic.SemanticException;
import core.syntatic.SyntaticAnalyser;


/**
 * Main class in the FORD Compiler. Its main method receives a list containing source 
 * code files names and produces one executable file to each one. 
 * 
 * @author Ricardo Ara�jo Santos
 */
public class Compiler {
	
	private static CodeGenerator cg;
	
	public static void main(String[] args) {
		if (args.length == 0){
			System.err.println("Correct usage: java Main Program1 ... ProgramN");
			System.exit(1);
		}
		for (String fileName : args) {
			try{
				FileReader source = new FileReader( new File( fileName ) );
				System.out.println("Compiling " + fileName);
				compile( source, fileName );
			}catch ( FileNotFoundException e ) {
				System.err.println("\n-------------------------------------------------------------------");
				System.err.println("There's no file named: " + fileName);
				System.err.println("Please check if it exists before running FORD Compiler on it again.");
				e.printStackTrace();
				System.err.println("-------------------------------------------------------------------\n");
			} catch (IOException e) {
				System.err.println("\n-------------------------------------------------------------------");
				System.err.println("There were problems during the reading of the file: " + fileName);
				e.printStackTrace();
				System.err.println("-------------------------------------------------------------------\n");
			} catch (SyntaxException e) {
				System.err.println("\n-------------------------------------------------------------------");
				System.err.println("Syntax Error: " + e.getMessage());
				e.printStackTrace();
				System.err.println("-------------------------------------------------------------------\n");
			}
		}
	}
	
	/**
	 * It compiles a source file written in FORD language and produces a executable file
	 * in a lower level language 
	 * 
	 * TODO Say which language it is. 
	 * 
	 * @param source The source file to compile.
	 * @param fileName The source file name.
	 * @throws SyntaxException 
	 * @throws IOException 
	 * @throws FileNotFoundException When there is not such file in the file system. It should 
	 * never launch as it has already been checked in the main method.
	 */
	private static void compile( FileReader source, String fileName ) throws IOException, SyntaxException{
		ErrorHandler errorHandler = new ErrorHandler();
		LexicalAnalyser scanner = new LexicalAnalyser( source, errorHandler );
		SyntaticAnalyser parser = new SyntaticAnalyser( scanner );
		cg = CodeGenerator.getInstace();
		cg.init(fileName.substring(9, fileName.length() - 5));
		
		try {
			parser.analyseIt();	
			
		} catch (SemanticException e) {
			for (String error : e.getErrors()) {
				System.out.println("### ERROR at " + error);
			}
		} catch (Exception e) {
			System.out.println("### ERROR at line " + scanner.getLineNumber() + ": " + e.getMessage() + " ###");
			e.printStackTrace();
		}
	}
	
}
